#include <graphics.h>
#include <conio.h>
#include <string.h>
#pragma comment(lib,"Winmm.lib")
#define high 600
#define width 500
//全局变量
int end = 0;
int plane_x, plane_y;
int bullet_x, bullet_y;
int enemy_x, enemy_y;
int i;
char score[2] = { 0 };
IMAGE img_bk;	//定义IMAGE对象背景
IMAGE img_plane1, img_plane2;
IMAGE img_bullet1, img_bullet2;
IMAGE img_enemy1, img_enemy2;
void start()
{
	mciSendString("open D:\\game_music.mp3 alias bkmusic", NULL, 0, NULL);//背景音
	mciSendString("play bkmusic repeat", NULL, 0, NULL);//循环播放

	plane_x = width / 2;
	plane_y = 500;
	bullet_x = width;
	bullet_y = high;
	enemy_x = rand() % width;
	enemy_y = 0;
	initgraph(width, high);

	loadimage(&img_bk, "D:\\background.jpg");	//读取图片到img对象中
	loadimage(&img_plane1, "D:\\planeNormal_1.jpg");
	loadimage(&img_plane2, "D:\\planeNormal_2.jpg");
	loadimage(&img_bullet1, "D:\\bullet1.jpg");
	loadimage(&img_bullet2, "D:\\bullet2.jpg");
	loadimage(&img_enemy1, "D:\\enemyPlane1.jpg");
	loadimage(&img_enemy2, "D:\\enemyPlane2.jpg");
	BeginBatchDraw();
}

void show()
{
	putimage(0, 0, &img_bk);	//在坐标（0,0）位置显示IMAGE对象	
	putimage(enemy_x - 50, enemy_y - 60, &img_enemy1, NOTSRCERASE);
	putimage(enemy_x - 50, enemy_y - 60, &img_enemy2, SRCINVERT);

	putimage(plane_x - 50, plane_y - 60, &img_plane1, NOTSRCERASE);
	putimage(plane_x - 50, plane_y - 60, &img_plane2, SRCINVERT);

	putimage(bullet_x, bullet_y, &img_bullet1, NOTSRCERASE);//子弹
	putimage(bullet_x, bullet_y, &img_bullet2, SRCINVERT);

	outtextxy(width - 50, high + 20, "score");//显示文字

	FlushBatchDraw();
	Sleep(50);
}
void updatewithoutInput()
{
	if (bullet_y > -50)
		bullet_y -= 5;
	if (enemy_y < high + 50)
	{
		enemy_y += 3;
	}
	else
	{
		enemy_x = rand() % width;//出了画布，重新读取敌机
		enemy_y = -50;
	}
	if (abs(enemy_x - plane_x) + abs(enemy_y - plane_y) < 100)//如果敌机与我机相撞
	{
		IMAGE plane_explode1, plane_explode2;//爆炸效果
		loadimage(&plane_explode1, "D:\\planeExplode_1.jpg");
		loadimage(&plane_explode2, "D:\\planeExplode_2.jpg");
		putimage(plane_x - 50, plane_y - 60, &plane_explode1, NOTSRCERASE);
		putimage(plane_x - 50, plane_y - 60, &plane_explode2, SRCINVERT);
		//爆炸音效
		mciSendString("close explode", NULL, 0, NULL);//先把前一次背景乐关掉
		mciSendString("open D:\\explode.mp3 alias explode", NULL, 0, NULL);//打开跳动音乐
		mciSendString("play explode", NULL, 0, NULL);//仅播放一次
		end = 1;
	}
	if (abs(enemy_x - bullet_x) + abs(enemy_y - bullet_y) < 50)//如果敌机被击中
	{
		mciSendString("close gotEnemy", NULL, 0, NULL);//先把前一次背景乐关掉
		mciSendString("open D:\\gotEnemy.mp3 alias gotEnemy", NULL, 0, NULL);//打开跳动音乐
		mciSendString("play gotEnemy", NULL, 0, NULL);//仅播放一次

		enemy_y = -50;//重新读入敌机
		enemy_x = rand() % width;
	}
}
void updatewithInput()
{
	MOUSEMSG m;
	while (MouseHit())
	{
		m = GetMouseMsg();

		if (m.uMsg == WM_MOUSEMOVE)//鼠标移动时飞机的位置
		{
			plane_x = m.x;
			plane_y = m.y;
		}
		else if (m.uMsg == WM_LBUTTONDOWN)//按下鼠标左键发射子弹
		{
			bullet_x = plane_x;
			bullet_y = plane_y - 50;

			mciSendString("close f_gun", NULL, 0, NULL);//先把前一次背景乐关掉
			mciSendString("open D:\\f_gun.mp3 alias f_gun", NULL, 0, NULL);//打开跳动音乐
			mciSendString("play f_gun", NULL, 0, NULL);//仅播放一次
		}
	}
}
void gameover()
{
	EndBatchDraw();
	_getch();
	closegraph();
}
int main()
{
	start();//初始化
	while (1)//循环执行
	{
		show();//显示画面
		updatewithoutInput();
		updatewithInput();
		if (end == 1)//游戏结束
			break;
	}
	gameover();//游戏结束
	return 0;
}